package com.sld.concrete.register.controller;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import com.sld.concrete.algorithm.vo.WorkDataInfo;
import com.sld.concrete.vehicle.domain.RecDevWorkSequence;
import com.sld.concrete.vehicle.service.IRecDevWorkSequenceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.sld.concrete.register.domain.InfoConcreteRegister;
import com.sld.concrete.register.service.IInfoConcreteRegisterService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 混凝土信息登记Controller
 * 
 * @author topviewers
 * @date 2022-04-23
 */
@Controller
@RequestMapping("/concrete/register")
public class InfoConcreteRegisterController extends BaseController
{
    private String prefix = "concrete/register";

    @Resource
    private IInfoConcreteRegisterService infoConcreteRegisterService;

    @Resource
    IRecDevWorkSequenceService workSequenceService;

    @RequiresPermissions("concrete:register:view")
    @GetMapping()
    public String register()
    {
        return prefix + "/register";
    }


    @GetMapping("complement")
    public String complement() {
        return prefix + "/complement";
    }

    @GetMapping("/append/{seqId}")
    public String append(@PathVariable("seqId") Long seqId, ModelMap mmap)
    {
        RecDevWorkSequence sequence = workSequenceService.selectRecDevWorkSequenceById(seqId);
        mmap.addAttribute("seqId",seqId);
        mmap.addAttribute("plateNo",sequence.getPlateNo());
        return prefix + "/appendRegister";
    }

    /**
     * 查询混凝土信息登记列表
     */
    @RequiresPermissions("concrete:register:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InfoConcreteRegister infoConcreteRegister)
    {
        startPage();
        List<InfoConcreteRegister> list = infoConcreteRegisterService.selectInfoConcreteRegisterList(infoConcreteRegister);
        return getDataTable(list);
    }

    /**
     * 导出混凝土信息登记列表
     */
    @RequiresPermissions("concrete:register:export")
    @Log(title = "混凝土信息登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InfoConcreteRegister infoConcreteRegister)
    {
        List<InfoConcreteRegister> list = infoConcreteRegisterService.selectInfoConcreteRegisterList(infoConcreteRegister);
        ExcelUtil<InfoConcreteRegister> util = new ExcelUtil<InfoConcreteRegister>(InfoConcreteRegister.class);
        return util.exportExcel(list, "混凝土信息登记数据");
    }

    /**
     * 新增混凝土信息登记
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存混凝土信息登记
     */
    @RequiresPermissions("concrete:register:add")
    @Log(title = "混凝土信息登记", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(InfoConcreteRegister infoConcreteRegister){
        try {
            int result = infoConcreteRegisterService.insertInfoConcreteRegister(infoConcreteRegister);
            return toAjax(result);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改混凝土信息登记
     */
    @RequiresPermissions("concrete:register:edit")
    @GetMapping("/edit/{registerId}")
    public String edit(@PathVariable("registerId") Long registerId, ModelMap mmap)
    {
        InfoConcreteRegister infoConcreteRegister = infoConcreteRegisterService.selectInfoConcreteRegisterByRegisterId(registerId);
        mmap.put("infoConcreteRegister", infoConcreteRegister);
        return prefix + "/edit";
    }

    /**
     * 修改保存混凝土信息登记
     */
    @RequiresPermissions("concrete:register:edit")
    @Log(title = "混凝土信息登记", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(InfoConcreteRegister infoConcreteRegister)
    {
        return toAjax(infoConcreteRegisterService.updateInfoConcreteRegister(infoConcreteRegister));
    }

    /**
     * 删除混凝土信息登记
     */
    @RequiresPermissions("concrete:register:remove")
    @Log(title = "混凝土信息登记", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(infoConcreteRegisterService.deleteInfoConcreteRegisterByRegisterIds(ids));
    }

    /**
     * 查询没有登记信息的运输列表
     * 查询待补录信息
     */
    @PostMapping( "/queryUnbindSequence")
    @ResponseBody
    public TableDataInfo queryUnbindSequence(WorkDataInfo workData, String plateNo, String startTime, String endTime, Map<String, Object> params){
        startPage();
        List<WorkDataInfo> list = infoConcreteRegisterService.queryUnbindSequence(workData);
        return getDataTable(list);
    }


    /**
     * 补录指定工作序号的登记信息
     */
    @PostMapping("/saveSequenceRegisterInfo")
    @ResponseBody
    public AjaxResult saveSequenceRegisterInfo(String plateNo, String seqId, String concreteType,
                                               String concreteVolume, String companyName,String mixingStation,
                                               String machineUnit, String slumpStart, String slumpEnd) {
        int volume = 0, start = 0, end = 0;
        if (StringUtils.isNotEmpty(concreteVolume)) {
            volume = Integer.parseInt(concreteVolume);
        }
        if (StringUtils.isNotEmpty(slumpStart)) {
            start = Integer.parseInt(slumpStart);

        }
        if (StringUtils.isNotEmpty(slumpEnd)) {
            end = Integer.parseInt(slumpEnd);
        }
        try {
            return toAjax(infoConcreteRegisterService.saveSequenceRegisterInfo(seqId, plateNo, concreteType, volume, companyName, mixingStation, machineUnit, start, end));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
